/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.cruisewatcher;

import com.intellij.openapi.util.DefaultJDOMExternalizer;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import org.jdom.Element;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CruiseWatcherSettings implements JDOMExternalizable {
    public String baseUrl;
    public String baseImageFolder;
    public int refreshInterval = 1;
    public boolean disableProxy;
    private List cruiseBuilds = new ArrayList();

    public Iterator iterator() {
        return cruiseBuilds.iterator();
    }

    public List getBuilds() {
        ArrayList arrayList = new ArrayList();
        arrayList.addAll(cruiseBuilds);
        return arrayList;
    }

    public void setBuilds(List newToggles) {
        cruiseBuilds.clear();
        cruiseBuilds.addAll(newToggles);
    }

    public void readExternal(org.jdom.Element element) throws InvalidDataException {
        DefaultJDOMExternalizer.readExternal(this, element);
        cruiseBuilds.clear();
        List children = element.getChildren("build");
        for (Iterator iterator = children.iterator(); iterator.hasNext();) {
            Element child = (Element) iterator.next();
            String name = child.getAttributeValue("name");
            String rssUrl = child.getAttributeValue("rssUrl");
            String resultUrl = child.getAttributeValue("resultUrl");
            String brokenFolder = child.getAttributeValue("brokenFolder");
            String fixedFolder = child.getAttributeValue("fixedFolder");
            cruiseBuilds.add(new CruiseBuild(name, rssUrl, resultUrl, brokenFolder, fixedFolder));
        }
    }

    public void writeExternal(org.jdom.Element element) throws WriteExternalException {
        DefaultJDOMExternalizer.writeExternal(this, element);
        for (Iterator iterator = cruiseBuilds.iterator(); iterator.hasNext();) {
            CruiseBuild toggleRow = (CruiseBuild) iterator.next();
            Element elem = new Element("build");
            elem.setAttribute("name", toggleRow.getName());
            elem.setAttribute("rssUrl", toggleRow.getRssUrl());
            elem.setAttribute("resultUrl", toggleRow.getResultUrl());
            elem.setAttribute("brokenFolder", toggleRow.getBrokenFolder());
            elem.setAttribute("fixedFolder", toggleRow.getFixedFolder());
            element.addContent(elem);
        }
    }
}
